
package com.hlkj.pay.app.merchant.ext.impl.hnapay.utils;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.ext.dto.access.MerchantAccessCallBackDto;
import com.hlkj.pay.app.merchant.ext.dto.access.MerchantBusinessCallBackDto;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.dto.HnapayConfig;
import com.hlkj.pay.enums.MerchantEnum;

import java.util.Map;

/**
 * @author HlpayTeam
 * @date 2024/10/09 10:02
 */
public class HnapayCallBackParamsParseUtil {

    /**
     * 进件回调参数解析
     *
     * @param callBackBody
     * @return
     */
    public static MerchantAccessCallBackDto accessCallBackParamsParse(String callBackBody, HnapayConfig payConfig) {
        MerchantAccessCallBackDto merchantAccessCallBackDto = new MerchantAccessCallBackDto();
        Map<String, String> map = JsonUtils.parseObject(callBackBody, Map.class);
        String mac = map.remove("MAC");
        // 参数组装
        String sign = "";
        if (!mac.equals(sign)) {
            merchantAccessCallBackDto.setProcessStatus(false);
            merchantAccessCallBackDto.setRemark("验签失败");
            return merchantAccessCallBackDto;
        }
        if ("AGMERAPPLY".equals(map.get("recentMessType"))) {
            merchantAccessCallBackDto.setProcessStatus(false);
            merchantAccessCallBackDto.setRemark("方法调用错误");
            return merchantAccessCallBackDto;
        }
        // 参数组装
        String reqSeqId = map.get("operaTrace");
        merchantAccessCallBackDto.setRequestId(reqSeqId);
        String auditStatus = map.get("auditStatus");
        switch (auditStatus) {
            case "0":
                //成功
                merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                break;
            case "5":
                //成功
                merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SIGN);
                break;
            case "X":
            case "1":
                //失败
                merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
                break;
            default:
                //审核中
                merchantAccessCallBackDto.setAccessStatus(MerchantEnum.ACCESS_STATUS.CHANNEL_APPROVE);
                break;
        }
        return merchantAccessCallBackDto;
    }

    /**
     * 进件回调参数解析
     *
     * @param payConfig
     * @param callBackBody
     * @return
     */
    public static boolean verify(HnapayConfig payConfig, String callBackBody) throws Exception {
        // 参数组装
        JSONObject response = JSON.parseObject(callBackBody);
        return RecepitUtil.verify(response, payConfig, response.getString("signValue"));
    }
}
